<!DOCTYPE html>
<html lang="en">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="../assets/site.css" rel="stylesheet">
  <title>testing</title>
  
    <meta name="twitter:title" content="Package testing">
    <meta property="og:title" content="Package testing">
    <meta name="description" content="Package testing provides support for automated testing of Go packages.">
    <meta name="twitter:description" content="Package testing provides support for automated testing of Go packages.">
    <meta property="og:description" content="Package testing provides support for automated testing of Go packages.">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:site" content="@golang">
  

</head>
<body>

<div class="container">

        <h2 id="pkg-overview">package testing</h2>

        <p><code>import "testing"</code>

        <p>
testing 提供对 Go 包的自动化测试的支持。通过 `go test` 命令，能够自动执行如下形式的任何函数：
</p>
<pre>func TestXxx(*testing.T)
</pre>
<p>
其中 Xxx 可以是任何字母数字字符串（但第一个字母不能是
[a-z]），用于识别测试例程。
</p>
<p>
在这些函数中，使用 Error, Fail 或相关方法来发出失败信号。
</p>
<p>
要编写一个新的测试套件，需要创建一个名称以 _test.go 结尾的文件，该文件包含 `TestXxx` 函数，如上所述。 将该文件放在与被测试的包相同的包中。该文件将被排除在正常的程序包之外，但在运行 “go test” 命令时将被包含。 有关详细信息，请运行 “go help test” 和 “go help testflag” 了解。
</p>
<p>
如果有需要，可以调用 *T 和 *B 的 Skip 方法，跳过该测试或基准测试：
</p>
<pre>func TestTimeConsuming(t *testing.T) {
    if testing.Short() {
        t.Skip(&#34;skipping test in short mode.&#34;)
    }
    ...
}
</pre>
<h4 id="hdr-Benchmarks">Benchmarks <a class="permalink" href="#hdr-Benchmarks">&para</a></h4>
<p>
如下形式的函数：
</p>
<pre>func BenchmarkXxx(*testing.B)
</pre>
<p>
被认为是基准测试，通过 &#34;go test&#34; 命令，加上 -bench flag 来执行。多个基准测试按照顺序运行。
</p>
<p>
testing flags 的详细描述, 参见
<a href="https://github.com/golang/go/blob/master/cmd/go/#hdr-Description_of_testing_flags">https://github.com/golang/go/blob/master/cmd/go/#hdr-Description_of_testing_flags</a>.
</p>
<p>
基准测试函数样例看起来如下所示：
</p>
<pre>func BenchmarkHello(b *testing.B) {
    for i := 0; i &lt; b.N; i++ {
        fmt.Sprintf(&#34;hello&#34;)
    }
}
</pre>
<p>
基准函数会运行目标代码 b.N 次。在基准执行期间，会调整 b.N 直到基准测试函数持续足够长的时间。输出
</p>
<pre>BenchmarkHello    10000000    282 ns/op
</pre>
<p>
意味着循环执行了 10000000 次，每次循环花费 282 纳秒(ns)。
</p>
<p>
如果在运行前基准测试需要一些耗时的配置，则可以先重置定时器：
</p>
<pre>func BenchmarkBigLen(b *testing.B) {
    big := NewBig()
    b.ResetTimer()
    for i := 0; i &lt; b.N; i++ {
        big.Len()
    }
}
</pre>
<p>
如果基准测试需要在并行设置中测试性能，则可以使用 RunParallel 辅助函数; 这样的基准测试一般与 go test -cpu 标志一起使用：
</p>
<pre>func BenchmarkTemplateParallel(b *testing.B) {
    templ := template.Must(template.New(&#34;test&#34;).Parse(&#34;Hello, {{.}}!&#34;))
    b.RunParallel(func(pb *testing.PB) {
        var buf bytes.Buffer
        for pb.Next() {
            buf.Reset()
            templ.Execute(&amp;buf, &#34;World&#34;)
        }
    })
}
</pre>
<h4 id="hdr-Examples">Examples <a class="permalink" href="#hdr-Examples">&para</a></h4>
<p>
该包还运行并验证示例代码。示例函数可以包括以 "Output：" 开头的行注释，并在运行测试时与函数的标准输出进行比较。 （比较时会忽略前导和尾随空格。）这些是一个 example 的例子：
</p>
<pre>func ExampleHello() {
        fmt.Println(&#34;hello&#34;)
        // Output: hello
}

func ExampleSalutations() {
        fmt.Println(&#34;hello, and&#34;)
        fmt.Println(&#34;goodbye&#34;)
        // Output:
        // hello, and
        // goodbye
}
</pre>
<p>
"Unordered output:" 形式的注释，和 "Output:" 类似，但是能够以任意顺序匹配行：
</p>
<pre>
func ExamplePerm() {
    for _, value := range Perm(4) {
        fmt.Println(value)
    }
    // Unordered output: 4
    // 2
    // 1
    // 3
    // 0
}
</pre>
<p>
没有输出注释的示例函数被编译但不执行。
</p>
<p>
example 声明的命名约定：包，函数 F，类型 T，类型 T 上的方法 M 依次是：
</p>
<pre>func Example() { ... }
func ExampleF() { ... }
func ExampleT() { ... }
func ExampleT_M() { ... }
</pre>
<p>
可以为 包/类型/函数/方法 提供多个 example 函数，这通过在名称上附加一个不同的后缀来实现。后缀必须是以小写字母开头。
</p>
<pre>func Example_suffix() { ... }
func ExampleF_suffix() { ... }
func ExampleT_suffix() { ... }
func ExampleT_M_suffix() { ... }
</pre>
<p>
当一个文件包含一个示例函数，同时至少一个其他函数，类型，变量或常量声明，或没有测试或基准函数时，这个测试文件作为示例存在，通常命名为 example_test.go
</p>
<h4 id="hdr-Subtests_and_Sub_benchmarks">Subtests 和 Sub-benchmarks <a class="permalink" href="#hdr-Subtests_and_Sub_benchmarks">&para</a></h4>
<p>
T 和 B 的 Run 方法允许定义子单元测试和子基准测试，而不必为每个子测试和子基准定义单独的函数。这使得可以使用 Table-Driven 的基准测试和创建层级测试。它还提供了一种共享通用 setup 和 tear-down 代码的方法：
</p>
<pre>func TestFoo(t *testing.T) {
    // &lt;setup code&gt;
    t.Run(&#34;A=1&#34;, func(t *testing.T) { ... })
    t.Run(&#34;A=2&#34;, func(t *testing.T) { ... })
    t.Run(&#34;B=1&#34;, func(t *testing.T) { ... })
    // &lt;tear-down code&gt;
}
</pre>
<p>
每个子测试和子基准测试都有一个唯一的名称：顶级测试的名称和传递给 Run 的名称的组合，以斜杠分隔，并具有用于消歧的可选尾随序列号。
</p>
<p>
-run 和 -bench 命令行标志的参数是与测试名称相匹配的非固定的正则表达式。对于具有多个斜杠分隔元素（例如子测试）的测试，该参数本身是斜杠分隔的，其中表达式依次匹配每个名称元素。因为它是非固定的，一个空的表达式匹配任何字符串。例如，使用 &#34;匹配&#34; 表示 &#34;其名称包含&#34;：
</p>
<pre>go test -run &#39;&#39;      # Run 所有测试。
go test -run Foo     # Run 匹配 "Foo" 的顶层测试，例如 "TestFooBar"。
go test -run Foo/A=  # 匹配顶层测试 "Foo"，运行其匹配 "A=" 的子测试。
go test -run /A=1    # 运行所有匹配 "A=1" 的子测试。
</pre>
<p>
子测试也可用于控制并行性。所有的子测试完成后，父测试才会完成。在这个例子中，所有的测试是相互并行运行的，当然也只是彼此之间，不包括定义在其他顶层测试的子测试：
</p>
<pre>func TestGroupedParallel(t *testing.T) {
    for _, tc := range tests {
        tc := tc // capture range variable
        t.Run(tc.Name, func(t *testing.T) {
            t.Parallel()
            ...
        })
    }
}
</pre>
<p>
在并行子测试完成之前，Run 方法不会返回，这提供了一种测试后清理的方法：
</p>
<pre>func TestTeardownParallel(t *testing.T) {
    // This Run will not return until the parallel tests finish.
    t.Run(&#34;group&#34;, func(t *testing.T) {
        t.Run(&#34;Test1&#34;, parallelTest1)
        t.Run(&#34;Test2&#34;, parallelTest2)
        t.Run(&#34;Test3&#34;, parallelTest3)
    })
    // &lt;tear-down code&gt;
}
</pre>
<h4 id="hdr-Main">Main <a class="permalink" href="#hdr-Main">&para</a></h4>
<p>
测试程序有时需要在测试之前或之后进行额外的设置（setup）或拆卸（teardown）。有时, 测试还需要控制在主线程上运行的代码。为了支持这些和其他一些情况, 如果测试文件包含函数:
</p>
<pre>func TestMain(m *testing.M)
</pre>
<p>
那么生成的测试将调用 TestMain(m)，而不是直接运行测试。TestMain 运行在主 goroutine 中, 可以在调用 m.Run 前后做任何设置和拆卸。应该使用 m.Run 的返回值作为参数调用 os.Exit。在调用 TestMain 时, flag.Parse 并没有被调用。所以，如果 TestMain 依赖于 command-line 标志 (包括 testing 包的标记), 则应该显示的调用 flag.Parse。
</p>
<p>
一个简单的 TestMain 的实现：
</p>
<pre>func TestMain(m *testing.M) {
	// call flag.Parse() here if TestMain uses flags
    // 如果 TestMain 使用了 flags，这里应该加上 flag.Parse()
	os.Exit(m.Run())
}</pre>


        
  
        <h3 id="pkg-index" class="section-header">Index <a class="permalink" href="#pkg-index">&para;</a></h3>

        

        <ul class="list-unstyled">
          
          
          <li><a href="#AllocsPerRun">func AllocsPerRun(runs int, f func()) (avg float64)</a></li><li><a href="#CoverMode">func CoverMode() string</a></li><li><a href="#Coverage">func Coverage() float64</a></li><li><a href="#Main">func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample)</a></li><li><a href="#RegisterCover">func RegisterCover(c Cover)</a></li><li><a href="#RunBenchmarks">func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark)</a></li><li><a href="#RunExamples">func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool)</a></li><li><a href="#RunTests">func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool)</a></li><li><a href="#Short">func Short() bool</a></li><li><a href="#Verbose">func Verbose() bool</a></li>
          
            <li><a href="#B">type B</a></li>
            <ul>
            
            <li><a href="#B.Error">func (c *B) Error(args ...interface{})</a></li><li><a href="#B.Errorf">func (c *B) Errorf(format string, args ...interface{})</a></li><li><a href="#B.Fail">func (c *B) Fail()</a></li><li><a href="#B.FailNow">func (c *B) FailNow()</a></li><li><a href="#B.Failed">func (c *B) Failed() bool</a></li><li><a href="#B.Fatal">func (c *B) Fatal(args ...interface{})</a></li><li><a href="#B.Fatalf">func (c *B) Fatalf(format string, args ...interface{})</a></li><li><a href="#B.Log">func (c *B) Log(args ...interface{})</a></li><li><a href="#B.Logf">func (c *B) Logf(format string, args ...interface{})</a></li><li><a href="#B.Name">func (c *B) Name() string</a></li><li><a href="#B.ReportAllocs">func (b *B) ReportAllocs()</a></li><li><a href="#B.ResetTimer">func (b *B) ResetTimer()</a></li><li><a href="#B.Run">func (b *B) Run(name string, f func(b *B)) bool</a></li><li><a href="#B.RunParallel">func (b *B) RunParallel(body func(*PB))</a></li><li><a href="#B.SetBytes">func (b *B) SetBytes(n int64)</a></li><li><a href="#B.SetParallelism">func (b *B) SetParallelism(p int)</a></li><li><a href="#B.Skip">func (c *B) Skip(args ...interface{})</a></li><li><a href="#B.SkipNow">func (c *B) SkipNow()</a></li><li><a href="#B.Skipf">func (c *B) Skipf(format string, args ...interface{})</a></li><li><a href="#B.Skipped">func (c *B) Skipped() bool</a></li><li><a href="#B.StartTimer">func (b *B) StartTimer()</a></li><li><a href="#B.StopTimer">func (b *B) StopTimer()</a></li>
            </ul>
          
            <li><a href="#BenchmarkResult">type BenchmarkResult</a></li>
            <ul>
            <li><a href="#Benchmark">func Benchmark(f func(b *B)) BenchmarkResult</a></li>
            <li><a href="#BenchmarkResult.AllocedBytesPerOp">func (r BenchmarkResult) AllocedBytesPerOp() int64</a></li><li><a href="#BenchmarkResult.AllocsPerOp">func (r BenchmarkResult) AllocsPerOp() int64</a></li><li><a href="#BenchmarkResult.MemString">func (r BenchmarkResult) MemString() string</a></li><li><a href="#BenchmarkResult.NsPerOp">func (r BenchmarkResult) NsPerOp() int64</a></li><li><a href="#BenchmarkResult.String">func (r BenchmarkResult) String() string</a></li>
            </ul>
          
            <li><a href="#Cover">type Cover</a></li>
            
            
            
            
          
            <li><a href="#CoverBlock">type CoverBlock</a></li>
            
            
            
            
          
            <li><a href="#InternalBenchmark">type InternalBenchmark</a></li>
            
            
            
            
          
            <li><a href="#InternalExample">type InternalExample</a></li>
            
            
            
            
          
            <li><a href="#InternalTest">type InternalTest</a></li>
            
            
            
            
          
            <li><a href="#M">type M</a></li>
            <ul>
            <li><a href="#MainStart">func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) *M</a></li>
            <li><a href="#M.Run">func (m *M) Run() int</a></li>
            </ul>
          
            <li><a href="#PB">type PB</a></li>
            <ul>
            
            <li><a href="#PB.Next">func (pb *PB) Next() bool</a></li>
            </ul>
          
            <li><a href="#T">type T</a></li>
            <ul>
            
            <li><a href="#T.Error">func (c *T) Error(args ...interface{})</a></li><li><a href="#T.Errorf">func (c *T) Errorf(format string, args ...interface{})</a></li><li><a href="#T.Fail">func (c *T) Fail()</a></li><li><a href="#T.FailNow">func (c *T) FailNow()</a></li><li><a href="#T.Failed">func (c *T) Failed() bool</a></li><li><a href="#T.Fatal">func (c *T) Fatal(args ...interface{})</a></li><li><a href="#T.Fatalf">func (c *T) Fatalf(format string, args ...interface{})</a></li><li><a href="#T.Log">func (c *T) Log(args ...interface{})</a></li><li><a href="#T.Logf">func (c *T) Logf(format string, args ...interface{})</a></li><li><a href="#T.Name">func (c *T) Name() string</a></li><li><a href="#T.Parallel">func (t *T) Parallel()</a></li><li><a href="#T.Run">func (t *T) Run(name string, f func(t *T)) bool</a></li><li><a href="#T.Skip">func (c *T) Skip(args ...interface{})</a></li><li><a href="#T.SkipNow">func (c *T) SkipNow()</a></li><li><a href="#T.Skipf">func (c *T) Skipf(format string, args ...interface{})</a></li><li><a href="#T.Skipped">func (c *T) Skipped() bool</a></li>
            </ul>
          
            <li><a href="#TB">type TB</a></li>
            
            
            
            
          
          
        </ul>

        
        
          <h4 id="pkg-examples">Examples <a class="permalink" href="#pkg-examples">&para;</a></h4>
          <ul class="list-unstyled">
            <li><a href="#example-B-RunParallel" onclick="$('#ex-B-RunParallel').addClass('in').removeClass('collapse').height('auto')">B.RunParallel</a></li>
          </ul>
        

        
        
<h4 id="pkg-files">
  <a href="https://github.com/golang/go/blob/master/src/testing/">Package Files</a>
  <a class="permalink" href="#pkg-files">&para;</a>
</h4>

<p><a href="https://github.com/golang/go/blob/master/src/testing/allocs.go">allocs.go</a> <a href="https://github.com/golang/go/blob/master/src/testing/benchmark.go">benchmark.go</a> <a href="https://github.com/golang/go/blob/master/src/testing/cover.go">cover.go</a> <a href="https://github.com/golang/go/blob/master/src/testing/example.go">example.go</a> <a href="https://github.com/golang/go/blob/master/src/testing/match.go">match.go</a> <a href="https://github.com/golang/go/blob/master/src/testing/testing.go">testing.go</a> </p>

        
        

        
        

        
        
        
          <h3 id="AllocsPerRun" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/allocs.go#L20">AllocsPerRun</a> <a class="permalink" href="#AllocsPerRun">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=AllocsPerRun&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/allocs.go#L20">❖</a><pre>func AllocsPerRun(runs <a href="/builtin#int">int</a>, f func()) (avg <a href="/builtin#float64">float64</a>)</pre></div><p>
AllocsPerRun 返回在调用 f 期间内存平均分配次数。虽然返回值的类型为 float64，但它始终是一个整数值。
</p>
<p>
要计算分配次数，该函数将首先作为热身运行一次。然后将测量并返回指定数量（runs 参数）运行的内存平均分配次数。
</p>
<p>
AllocsPerRun 在测量过程中将 GOMAXPROCS 设置为1，并在返回前将其还原。
</p>
  

        
          <h3 id="CoverMode" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L292">CoverMode</a> <a class="permalink" href="#CoverMode">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=CoverMode&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L292">❖</a><pre>func CoverMode() <a href="/builtin#string">string</a></pre></div><p>
CoverMode reports what the test coverage mode is set to. The
values are &#34;set&#34;, &#34;count&#34;, or &#34;atomic&#34;. The return value will be
empty if test coverage is not enabled.
</p>

          
  

        
          <h3 id="Coverage" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L45">Coverage</a> <a class="permalink" href="#Coverage">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Coverage&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L45">❖</a><pre>func Coverage() <a href="/builtin#float64">float64</a></pre></div><p>
Coverage reports the current code coverage as a fraction in the range [0, 1].
If coverage is not enabled, Coverage returns 0.
</p>
<p>
When running a large set of sequential test cases, checking Coverage after each one
can be useful for identifying which test cases exercise new code paths.
It is not a replacement for the reports generated by &#39;go test -cover&#39; and
&#39;go tool cover&#39;.
</p>

          
  

        
          <h3 id="Main" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L774">Main</a> <a class="permalink" href="#Main">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Main&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L774">❖</a><pre>func Main(matchString func(pat, str <a href="/builtin#string">string</a>) (<a href="/builtin#bool">bool</a>, <a href="/builtin#error">error</a>), tests []<a href="#InternalTest">InternalTest</a>, benchmarks []<a href="#InternalBenchmark">InternalBenchmark</a>, examples []<a href="#InternalExample">InternalExample</a>)</pre></div><p>
Main is an internal function, part of the implementation of the &#34;go test&#34; command.
It was exported because it is cross-package and predates &#34;internal&#34; packages.
It is no longer used by &#34;go test&#34; but preserved, as much as possible, for other
systems that simulate &#34;go test&#34; using Main, but Main sometimes cannot be updated as
new functionality is added to the testing package.
Systems simulating &#34;go test&#34; should be updated to use MainStart.
</p>

          
  

        
          <h3 id="RegisterCover" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L64">RegisterCover</a> <a class="permalink" href="#RegisterCover">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=RegisterCover&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L64">❖</a><pre>func RegisterCover(c <a href="#Cover">Cover</a>)</pre></div><p>
RegisterCover records the coverage data accumulators for the tests.
NOTE: This function is internal to the testing infrastructure and may change.
It is not covered (yet) by the Go 1 compatibility guidelines.
</p>

          
  

        
          <h3 id="RunBenchmarks" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L365">RunBenchmarks</a> <a class="permalink" href="#RunBenchmarks">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=RunBenchmarks&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L365">❖</a><pre>func RunBenchmarks(matchString func(pat, str <a href="/builtin#string">string</a>) (<a href="/builtin#bool">bool</a>, <a href="/builtin#error">error</a>), benchmarks []<a href="#InternalBenchmark">InternalBenchmark</a>)</pre></div><p>
An internal function but exported because it is cross-package; part of the implementation
of the &#34;go test&#34; command.
</p>

          
  

        
          <h3 id="RunExamples" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/example.go#L26">RunExamples</a> <a class="permalink" href="#RunExamples">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=RunExamples&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/example.go#L26">❖</a><pre>func RunExamples(matchString func(pat, str <a href="/builtin#string">string</a>) (<a href="/builtin#bool">bool</a>, <a href="/builtin#error">error</a>), examples []<a href="#InternalExample">InternalExample</a>) (ok <a href="/builtin#bool">bool</a>)</pre></div><p>
An internal function but exported because it is cross-package; part of the implementation
of the &#34;go test&#34; command.
</p>

          
  

        
          <h3 id="RunTests" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L858">RunTests</a> <a class="permalink" href="#RunTests">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=RunTests&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L858">❖</a><pre>func RunTests(matchString func(pat, str <a href="/builtin#string">string</a>) (<a href="/builtin#bool">bool</a>, <a href="/builtin#error">error</a>), tests []<a href="#InternalTest">InternalTest</a>) (ok <a href="/builtin#bool">bool</a>)</pre></div><p>
An internal function but exported because it is cross-package; part of the implementation
of the &#34;go test&#34; command.
</p>

          
  

        
          <h3 id="Short" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L285">Short</a> <a class="permalink" href="#Short">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Short&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L285">❖</a><pre>func Short() <a href="/builtin#bool">bool</a></pre></div><p>
Short reports whether the -test.short flag is set.
</p>

          
  

        
          <h3 id="Verbose" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L297">Verbose</a> <a class="permalink" href="#Verbose">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Verbose&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L297">❖</a><pre>func Verbose() <a href="/builtin#bool">bool</a></pre></div><p>
Verbose reports whether the -test.v flag is set.
</p>

          
  

        

        
        

        
          <h3 id="B" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L48">B</a> <a class="permalink" href="#B">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=B&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L48">❖</a><pre>type B struct {
    <span id="B.N">N</span> <a href="/builtin#int">int</a>
    <span class="com">// contains filtered or unexported fields</span>
}</pre></div><p>
B 是传递给基准测试函数的一种类型，它用于管理基准测试的计时行为，并指示应该迭代地运行测试多少次。
</p>
<p>
一个基准测试在它的基准测试函数返回时，又或者在它的基准测试函数调用 FailNow、Fatal、Fatalf、SkipNow、Skip 或者 Skipf 中的任意一个方法时，测试即宣告结束。至于其他报告方法，比如 Log 和 Error 的变种，则可以在其他 goroutine 中同时进行调用。
</p>
<p>
跟单元测试一样，基准测试会在执行的过程中积累日志，并在测试完毕时将日志转储到标准错误。但跟单元测试不一样的是，为了避免基准测试的结果受到日志打印操作的影响，基准测试总是会把日志打印出来。
</p>

          
          
            <h4 id="B.Error" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L510">Error</a> <a class="permalink" href="#B.Error">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FError&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L510">❖</a><pre>func (c *B) Error(args ...interface{})</pre></div><p>
调用 Error 相当于在调用 Log 之后调用 Fail 。
</p>

            
  

          
            <h4 id="B.Errorf" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L516">Errorf</a> <a class="permalink" href="#B.Errorf">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FErrorf&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L516">❖</a><pre>func (c *B) Errorf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
调用 Errorf 相当于在调用 Logf 之后调用 Fail 。
</p>

            
  

          
            <h4 id="B.Fail" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L437">Fail</a> <a class="permalink" href="#B.Fail">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFail&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L437">❖</a><pre>func (c *B) Fail()</pre></div><p>
将当前的测试函数标识为“失败”，但仍然继续执行该函数。
</p>

            
  

          
            <h4 id="B.FailNow" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L463">FailNow</a> <a class="permalink" href="#B.FailNow">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFailNow&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L463">❖</a><pre>func (c *B) FailNow()</pre></div><p>
将当前的测试函数标识为“失败”，并停止执行该函数。在此之后，测试过程将在下一个测试或者下一个基准测试中继续。

FailNow 必须在运行测试函数或者基准测试函数的 goroutine 中调用，而不能在测试期间创建的 goroutine 中调用。调用 FailNow 不会导致其他 goroutine 停止。
</p>

            
  

          
            <h4 id="B.Failed" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L451">Failed</a> <a class="permalink" href="#B.Failed">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFailed&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L451">❖</a><pre>func (c *B) Failed() <a href="/builtin#bool">bool</a></pre></div><p>
Failed 用于报告测试函数是否已失败。
</p>

            
  

          
            <h4 id="B.Fatal" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L522">Fatal</a> <a class="permalink" href="#B.Fatal">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFatal&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L522">❖</a><pre>func (c *B) Fatal(args ...interface{})</pre></div><p>
调用 Fatal 相当于在调用 Log 之后调用 FailNow 。
</p>

            
  

          
            <h4 id="B.Fatalf" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L528">Fatalf</a> <a class="permalink" href="#B.Fatalf">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFatalf&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L528">❖</a><pre>func (c *B) Fatalf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
调用 Fatalf 相当于在调用 Logf 之后调用 FailNow 。
</p>

            
  

          
            <h4 id="B.Log" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L500">Log</a> <a class="permalink" href="#B.Log">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FLog&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L500">❖</a><pre>func (c *B) Log(args ...interface{})</pre></div><p>
Log 使用与 Println 相同的格式化语法对它的参数进行格式化， 然后将格式化后的文本记录到错误日志里面：
</p>
<p>
对于测试来说， 格式化文本只会在测试失败或者设置了 -test.v 标志的情况下被打印出来；
对于基准测试来说， 为了避免 -test.v 标志的值对测试的性能产生影响， 格式化文本总会被打印出来。
</p>

            
  

          
            <h4 id="B.Logf" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L507">Logf</a> <a class="permalink" href="#B.Logf">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FLogf&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L507">❖</a><pre>func (c *B) Logf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
Log 使用与 Printf 相同的格式化语法对它的参数进行格式化， 然后将格式化后的文本记录到错误日志里面。 如果输入的格式化文本最末尾没有出现新行， 那么将一个新行添加到格式化后的文本末尾。
</p>
<p>
对于测试来说，Logf 产生的格式化文本只会在测试失败或者设置了 -test.v 标志的情况下被打印出来； 对于基准测试来说， 为了避免 -test.v 标志的值对测试的性能产生影响， Logf 产生的格式化文本总会被打印出来。
</p>

            
  

          
            <h4 id="B.Name" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L423">Name</a> <a class="permalink" href="#B.Name">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FName&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L423">❖</a><pre>func (c *B) Name() <a href="/builtin#string">string</a></pre></div><p>
返回正在运行的测试或者基准测试的名字。
</p>

            
  

          
            <h4 id="B.ReportAllocs" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L117">ReportAllocs</a> <a class="permalink" href="#B.ReportAllocs">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FReportAllocs&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L117">❖</a><pre>func (b *<a href="#B">B</a>) ReportAllocs()</pre></div><p>
打开当前基准测试的内存统计功能，与使用 -test.benchmem 设置类似，但 ReportAllocs 只影响那些调用了该函数的基准测试。
</p>

            
  

          
            <h4 id="B.ResetTimer" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L98">ResetTimer</a> <a class="permalink" href="#B.ResetTimer">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FResetTimer&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L98">❖</a><pre>func (b *<a href="#B">B</a>) ResetTimer()</pre></div><p>
对已经逝去的基准测试时间以及内存分配计数器进行清零。对于正在运行中的计时器，这个方法不会产生任何效果。
</p>

            
  

          
            <h4 id="B.Run" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L466">Run</a> <a class="permalink" href="#B.Run">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FRun&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L466">❖</a><pre>func (b *<a href="#B">B</a>) Run(name <a href="/builtin#string">string</a>, f func(b *<a href="#B">B</a>)) <a href="/builtin#bool">bool</a></pre></div><p>
执行名字为 name 的子基准测试（subbenchmark）f ，并报告 f 在执行过程中是否出现了任何失败。
</p>
<p>
子基准测试跟其他普通的基准测试一样。一个调用了 Run 方法至少一次的基准测试将不会对其自身进行测量（measure），并且在 N 为 1 时， 这个基准测试将只会被执行一次。
</p>
<p>
Run 可以同时在多个 goroutine 中被调用，但这些调用必须发生在 b 的外部基准函数（outer benchmark function）返回之前。
</p>

            
  

          
            <h4 id="B.RunParallel" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L573">RunParallel</a> <a class="permalink" href="#B.RunParallel">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FRunParallel&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L573">❖</a><pre>func (b *<a href="#B">B</a>) RunParallel(body func(*<a href="#PB">PB</a>))</pre></div><p>
以并行的方式执行给定的基准测试。 RunParallel 会创建出多个 goroutine ，并将 b.N 分配给这些 goroutine 执行， 其中 goroutine 数量的默认值为 GOMAXPROCS 。用户如果想要增加非CPU受限（non-CPU-bound）基准测试的并行性， 那么可以在 RunParallel 之前调用 SetParallelism 。RunParallel 通常会与 -cpu 标志一同使用。
</p>
<p>
body 函数将在每个 goroutine 中执行，这个函数需要设置所有 goroutine 本地的状态， 并迭代直到 pb.Next 返回 false 值为止。因为 StartTimer 、 StopTimer 和 ResetTimer 这三个函数都带有全局作用，所以 body 函数不应该调用这些函数；除此之外，body 函数也不应该调用 Run 函数。
</p>

            
  
    <div class="panel-group">
    
      <div class="panel panel-default" id="example-B-RunParallel">
        <div class="panel-heading"><a class="accordion-toggle" data-toggle="collapse" href="#ex-B-RunParallel">Example</a></div>
        <div id="ex-B-RunParallel" class="panel-collapse collapse"><div class="panel-body">
          
          <p>Code:<span class="pull-right"><a href="?play=B-RunParallel">play</a>&nbsp;</span>
          <pre>
<span class="com">// Parallel benchmark for text/template.Template.Execute on a single object.</span>
testing.Benchmark(func(b *testing.B) {
    templ := template.Must(template.New(&#34;test&#34;).Parse(&#34;Hello, {{.}}!&#34;))
    <span class="com">// RunParallel will create GOMAXPROCS goroutines
    // and distribute work among them.</span>
    b.RunParallel(func(pb *testing.PB) {
        <span class="com">// Each goroutine has its own bytes.Buffer.</span>
        var buf bytes.Buffer
        for pb.Next() {
            <span class="com">// The loop body is executed b.N times total across all goroutines.</span>
            buf.Reset()
            templ.Execute(&amp;buf, &#34;World&#34;)
        }
    })
})
</pre>
          
        </div></div>
      </div>
    
    </div>
  

          
            <h4 id="B.SetBytes" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L112">SetBytes</a> <a class="permalink" href="#B.SetBytes">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FSetBytes&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L112">❖</a><pre>func (b *<a href="#B">B</a>) SetBytes(n <a href="/builtin#int64">int64</a>)</pre></div><p>
记录在单个操作中处理的字节数量。在调用了这个方法之后，基准测试将会报告 ns/op 以及 MB/s 。
</p>

            
  

          
            <h4 id="B.SetParallelism" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L617">SetParallelism</a> <a class="permalink" href="#B.SetParallelism">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FSetParallelism&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L617">❖</a><pre>func (b *<a href="#B">B</a>) SetParallelism(p <a href="/builtin#int">int</a>)</pre></div><p>
将 RunParallel 使用的 goroutine 数量设置为 p*GOMAXPROCS ，如果 p 小于 1 ，那么调用将不产生任何效果。
</p>
<p>
CPU受限（CPU-bound）的基准测试通常不需要调用这个方法。
</p>

            
  

          
            <h4 id="B.Skip" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L534">Skip</a> <a class="permalink" href="#B.Skip">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkip&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L534">❖</a><pre>func (c *B) Skip(args ...interface{})</pre></div><p>
调用 Skip 相当于在调用 Log 之后调用 SkipNow 。
</p>

            
  

          
            <h4 id="B.SkipNow" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L552">SkipNow</a> <a class="permalink" href="#B.SkipNow">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkipNow&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L552">❖</a><pre>func (c *B) SkipNow()</pre></div><p>
将当前测试标识为“被跳过”并停止执行该测试。 如果一个测试在失败（参考 Error 、 Errorf 和 Fail）之后被跳过了， 那么它还是会被判断为是“失败的”。
</p>
<p>
在停止当前测试之后， 测试过程将在下一个测试或者下一个基准测试中继续， 具体请参考 FailNow 。
</p>
<p>
SkipNow 必须在运行测试的 goroutine 中进行调用， 而不能在测试期间创建的 goroutine 中调用。 调用 SkipNow 不会导致其他 goroutine 停止。
</p>

            
  

          
            <h4 id="B.Skipf" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L540">Skipf</a> <a class="permalink" href="#B.Skipf">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkipf&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L540">❖</a><pre>func (c *B) Skipf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
调用 Skipf 相当于在调用 Logf 之后调用 SkipNow 。
</p>

            
  

          
            <h4 id="B.Skipped" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L565">Skipped</a> <a class="permalink" href="#B.Skipped">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkipped&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L565">❖</a><pre>func (c *B) Skipped() <a href="/builtin#bool">bool</a></pre></div><p>
报告测试是否已被跳过。
</p>

            
  

          
            <h4 id="B.StartTimer" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L73">StartTimer</a> <a class="permalink" href="#B.StartTimer">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FStartTimer&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L73">❖</a><pre>func (b *<a href="#B">B</a>) StartTimer()</pre></div><p>
开始对测试进行计时。

这个函数在基准测试开始时会自动被调用， 它也可以在调用 StopTimer 之后恢复进行计时。
</p>

            
  

          
            <h4 id="B.StopTimer" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L86">StopTimer</a> <a class="permalink" href="#B.StopTimer">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FStopTimer&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L86">❖</a><pre>func (b *<a href="#B">B</a>) StopTimer()</pre></div><p>
停止对测试进行计时。

当你需要执行一些复杂的初始化操作， 并且你不想对这些操作进行测量时， 就可以使用这个方法来暂时地停止计时。
</p>

            
  

          
        
          <h3 id="BenchmarkResult" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L287">BenchmarkResult</a> <a class="permalink" href="#BenchmarkResult">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L287">❖</a><pre>type BenchmarkResult struct {
    <span id="BenchmarkResult.N">N</span>         <a href="/builtin#int">int</a>           <span class="com">// The number of iterations.</span>
    <span id="BenchmarkResult.T">T</span>         <a href="/time">time</a>.<a href="/time#Duration">Duration</a> <span class="com">// The total time taken.</span>
    <span id="BenchmarkResult.Bytes">Bytes</span>     <a href="/builtin#int64">int64</a>         <span class="com">// Bytes processed in one iteration.</span>
    <span id="BenchmarkResult.MemAllocs">MemAllocs</span> <a href="/builtin#uint64">uint64</a>        <span class="com">// The total number of memory allocations.</span>
    <span id="BenchmarkResult.MemBytes">MemBytes</span>  <a href="/builtin#uint64">uint64</a>        <span class="com">// The total number of bytes allocated.</span>
}</pre></div><p>
基准测试运行的结果。
</p>

          
          

          
            <h4 id="Benchmark" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L628">Benchmark</a> <a class="permalink" href="#Benchmark">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Benchmark&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L628">❖</a><pre>func Benchmark(f func(b *<a href="#B">B</a>)) <a href="#BenchmarkResult">BenchmarkResult</a></pre></div><p>
测试单个函数。用于创建不使用 &#34;go test&#34; 命令的自定义基准测试。
</p>
<p>
如果 f 调用 Run，则结果将是运行其所有子基准的结果估计，该子基准在单个基准测试中不会顺序调用 Run。
</p>

            
          
            <h4 id="BenchmarkResult.AllocedBytesPerOp" data-kind="m">func (BenchmarkResult) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L316">AllocedBytesPerOp</a> <a class="permalink" href="#BenchmarkResult.AllocedBytesPerOp">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult%2FAllocedBytesPerOp&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L316">❖</a><pre>func (r <a href="#BenchmarkResult">BenchmarkResult</a>) AllocedBytesPerOp() <a href="/builtin#int64">int64</a></pre></div>
            
  

          
            <h4 id="BenchmarkResult.AllocsPerOp" data-kind="m">func (BenchmarkResult) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L309">AllocsPerOp</a> <a class="permalink" href="#BenchmarkResult.AllocsPerOp">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult%2FAllocsPerOp&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L309">❖</a><pre>func (r <a href="#BenchmarkResult">BenchmarkResult</a>) AllocsPerOp() <a href="/builtin#int64">int64</a></pre></div>
            
  

          
            <h4 id="BenchmarkResult.MemString" data-kind="m">func (BenchmarkResult) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L343">MemString</a> <a class="permalink" href="#BenchmarkResult.MemString">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult%2FMemString&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L343">❖</a><pre>func (r <a href="#BenchmarkResult">BenchmarkResult</a>) MemString() <a href="/builtin#string">string</a></pre></div>
            
  

          
            <h4 id="BenchmarkResult.NsPerOp" data-kind="m">func (BenchmarkResult) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L295">NsPerOp</a> <a class="permalink" href="#BenchmarkResult.NsPerOp">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult%2FNsPerOp&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L295">❖</a><pre>func (r <a href="#BenchmarkResult">BenchmarkResult</a>) NsPerOp() <a href="/builtin#int64">int64</a></pre></div>
            
  

          
            <h4 id="BenchmarkResult.String" data-kind="m">func (BenchmarkResult) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L323">String</a> <a class="permalink" href="#BenchmarkResult.String">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult%2FString&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L323">❖</a><pre>func (r <a href="#BenchmarkResult">BenchmarkResult</a>) String() <a href="/builtin#string">string</a></pre></div>
            
  

          
        
          <h3 id="Cover" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L31">Cover</a> <a class="permalink" href="#Cover">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=Cover&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L31">❖</a><pre>type Cover struct {
    <span id="Cover.Mode">Mode</span>            <a href="/builtin#string">string</a>
    <span id="Cover.Counters">Counters</span>        map[<a href="/builtin#string">string</a>][]<a href="/builtin#uint32">uint32</a>
    <span id="Cover.Blocks">Blocks</span>          map[<a href="/builtin#string">string</a>][]<a href="#CoverBlock">CoverBlock</a>
    <span id="Cover.CoveredPackages">CoveredPackages</span> <a href="/builtin#string">string</a>
}</pre></div><p>
Cover records information about test coverage checking.
NOTE: This struct is internal to the testing infrastructure and may change.
It is not covered (yet) by the Go 1 compatibility guidelines.
</p>

          
          
          
  


          

          
        
          <h3 id="CoverBlock" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L18">CoverBlock</a> <a class="permalink" href="#CoverBlock">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=CoverBlock&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L18">❖</a><pre>type CoverBlock struct {
    <span id="CoverBlock.Line0">Line0</span> <a href="/builtin#uint32">uint32</a>
    <span id="CoverBlock.Col0">Col0</span>  <a href="/builtin#uint16">uint16</a>
    <span id="CoverBlock.Line1">Line1</span> <a href="/builtin#uint32">uint32</a>
    <span id="CoverBlock.Col1">Col1</span>  <a href="/builtin#uint16">uint16</a>
    <span id="CoverBlock.Stmts">Stmts</span> <a href="/builtin#uint16">uint16</a>
}</pre></div><p>
CoverBlock records the coverage data for a single basic block.
NOTE: This struct is internal to the testing infrastructure and may change.
It is not covered (yet) by the Go 1 compatibility guidelines.
</p>

          
          
          
  


          

          
        
          <h3 id="InternalBenchmark" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L30">InternalBenchmark</a> <a class="permalink" href="#InternalBenchmark">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=InternalBenchmark&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L30">❖</a><pre>type InternalBenchmark struct {
    <span id="InternalBenchmark.Name">Name</span> <a href="/builtin#string">string</a>
    <span id="InternalBenchmark.F">F</span>    func(b *<a href="#B">B</a>)
}</pre></div><p>
An internal type but exported because it is cross-package; part of the implementation
of the &#34;go test&#34; command.
</p>

          
          
          
  


          

          
        
          <h3 id="InternalExample" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/example.go#L17">InternalExample</a> <a class="permalink" href="#InternalExample">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=InternalExample&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/example.go#L17">❖</a><pre>type InternalExample struct {
    <span id="InternalExample.Name">Name</span>      <a href="/builtin#string">string</a>
    <span id="InternalExample.F">F</span>         func()
    <span id="InternalExample.Output">Output</span>    <a href="/builtin#string">string</a>
    <span id="InternalExample.Unordered">Unordered</span> <a href="/builtin#bool">bool</a>
}</pre></div>
          
          
          
  


          

          
        
          <h3 id="InternalTest" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L597">InternalTest</a> <a class="permalink" href="#InternalTest">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=InternalTest&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L597">❖</a><pre>type InternalTest struct {
    <span id="InternalTest.Name">Name</span> <a href="/builtin#string">string</a>
    <span id="InternalTest.F">F</span>    func(*<a href="#T">T</a>)
}</pre></div><p>
An internal type but exported because it is cross-package; part of the implementation
of the &#34;go test&#34; command.
</p>

          
          
          
  


          

          
        
          <h3 id="M" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L779">M</a> <a class="permalink" href="#M">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=M&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L779">❖</a><pre>type M struct {
    <span class="com">// contains filtered or unexported fields</span>
}</pre></div><p>
M 是传递给 TestMain 函数以运行实际测试的类型。
</p>

          
          
          
  


          
            <h4 id="MainStart" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L801">MainStart</a> <a class="permalink" href="#MainStart">&para;</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=MainStart&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L801">❖</a><pre>func MainStart(deps testDeps, tests []<a href="#InternalTest">InternalTest</a>, benchmarks []<a href="#InternalBenchmark">InternalBenchmark</a>, examples []<a href="#InternalExample">InternalExample</a>) *<a href="#M">M</a></pre></div><p>
MainStart is meant for use by tests generated by &#39;go test&#39;.
It is not meant to be called directly and is not subject to the Go 1 compatibility document.
It may change signature from release to release.
</p>

            
  

          

          
            <h4 id="M.Run" data-kind="m">func (*M) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L811">Run</a> <a class="permalink" href="#M.Run">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=M%2FRun&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L811">❖</a><pre>func (m *<a href="#M">M</a>) Run() <a href="/builtin#int">int</a></pre></div><p>
Run 运行这些测试。它返回要传递给 os.Exit 的退出代码。
</p>

            
  

          
        
          <h3 id="PB" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L540">PB</a> <a class="permalink" href="#PB">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=PB&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L540">❖</a><pre>type PB struct {
    <span class="com">// contains filtered or unexported fields</span>
}</pre></div><p>
PB 被 RunParallel 使用来运行并行基准测试。
</p>

          
          
          
  


          

          
            <h4 id="PB.Next" data-kind="m">func (*PB) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L548">Next</a> <a class="permalink" href="#PB.Next">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=PB%2FNext&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L548">❖</a><pre>func (pb *<a href="#PB">PB</a>) Next() <a href="/builtin#bool">bool</a></pre></div><p>
Next 判断是否有更多的迭代要执行
</p>

            
  

          
        
          <h3 id="T" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L414">T</a> <a class="permalink" href="#T">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=T&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L414">❖</a><pre>type T struct {
    <span class="com">// contains filtered or unexported fields</span>
}</pre></div><p>
T 是传递给测试函数的一种类型，它用于管理测试状态并支持格式化测试日志。测试日志会在执行测试的过程中不断累积， 并在测试完成时转储至标准输出。
</p>
<p>
当一个测试的测试函数返回时， 又或者当一个测试函数调用 FailNow 、 Fatal 、 Fatalf 、 SkipNow 、 Skip 或者 Skipf 中的任意一个时， 该测试即宣告结束。 跟 Parallel 方法一样， 以上提到的这些方法只能在运行测试函数的 goroutine 中调用。
</p>
<p>
至于其他报告方法， 比如 Log 以及 Error 的变种， 则可以在多个 goroutine 中同时进行调用。
</p>

            <h4 id="T.Error" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L510">Error</a> <a class="permalink" href="#T.Error">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FError&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L510">❖</a><pre>func (c *T) Error(args ...interface{})</pre></div><p>
调用 Error 相当于在调用 Log 之后调用 Fail 。
</p>

            <h4 id="T.Errorf" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L516">Errorf</a> <a class="permalink" href="#T.Errorf">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FErrorf&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L516">❖</a><pre>func (c *T) Errorf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
调用 Errorf 相当于在调用 Logf 之后调用 Fail 。
</p>

            
          
            <h4 id="T.Fail" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L437">Fail</a> <a class="permalink" href="#T.Fail">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFail&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L437">❖</a><pre>func (c *T) Fail()</pre></div><p>
将当前测试标识为失败，但是仍继续执行该测试。
</p>

            
  
            <h4 id="T.FailNow" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L463">FailNow</a> <a class="permalink" href="#T.FailNow">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFailNow&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L463">❖</a><pre>func (c *T) FailNow()</pre></div><p>
将当前测试标识为失败并停止执行该测试，在此之后，测试过程将在下一个测试或者下一个基准测试中继续。
</p>
<p>
FailNow 必须在运行测试函数或者基准测试函数的 goroutine 中调用，而不能在测试期间创建的 goroutine 中调用。调用 FailNow 不会导致其他 goroutine 停止。
</p>

            
  

          
            <h4 id="T.Failed" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L451">Failed</a> <a class="permalink" href="#T.Failed">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFailed&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L451">❖</a><pre>func (c *T) Failed() <a href="/builtin#bool">bool</a></pre></div><p>
Failed 用于报告测试函数是否已失败。
</p>

            
  

          
            <h4 id="T.Fatal" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L522">Fatal</a> <a class="permalink" href="#T.Fatal">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFatal&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L522">❖</a><pre>func (c *T) Fatal(args ...interface{})</pre></div><p>
调用 Fatal 相当于在调用 Log 之后调用 FailNow 。
</p>

            
  

          
            <h4 id="T.Fatalf" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L528">Fatalf</a> <a class="permalink" href="#T.Fatalf">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFatalf&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L528">❖</a><pre>func (c *T) Fatalf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
调用 Fatalf 相当于在调用 Logf 之后调用 FailNow 。
</p>

            
  

          
            <h4 id="T.Log" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L500">Log</a> <a class="permalink" href="#T.Log">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FLog&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L500">❖</a><pre>func (c *T) Log(args ...interface{})</pre></div><p>
Log 使用与 Println 相同的格式化语法对它的参数进行格式化，然后将格式化后的文本记录到错误日志里面：
</p>
<p>
1）对于测试来说，格式化文本只会在测试失败或者设置了 -test.v 标志的情况下被打印出来；
2）对于基准测试来说，为了避免 -test.v 标志的值对测试的性能产生影响， 格式化文本总会被打印出来。
</p>

            
  

          
            <h4 id="T.Logf" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L507">Logf</a> <a class="permalink" href="#T.Logf">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FLogf&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L507">❖</a><pre>func (c *T) Logf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
Log 使用与 Printf 相同的格式化语法对它的参数进行格式化，然后将格式化后的文本记录到错误日志里面。 如果输入的格式化文本最末尾没有出现新行，那么将一个新行添加到格式化后的文本末尾。
</p>
<p>
1）对于测试来说，Logf 产生的格式化文本只会在测试失败或者设置了 -test.v 标志的情况下被打印出来；
2）对于基准测试来说，为了避免 -test.v 标志的值对测试的性能产生影响，Logf 产生的格式化文本总会被打印出来。
</p>

            
  

          
            <h4 id="T.Name" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L423">Name</a> <a class="permalink" href="#T.Name">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FName&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L423">❖</a><pre>func (c *T) Name() <a href="/builtin#string">string</a></pre></div><p>
返回正在运行的测试或基准测试的名字。
</p>

            
  

          
            <h4 id="T.Parallel" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L573">Parallel</a> <a class="permalink" href="#T.Parallel">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=T%2FParallel&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L573">❖</a><pre>func (t *<a href="#T">T</a>) Parallel()</pre></div><p>
Parallel 用于表示当前测试只会与其他带有 Parallel 方法的测试并行进行测试。
</p>

            
  

          
            <h4 id="T.Run" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L666">Run</a> <a class="permalink" href="#T.Run">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=T%2FRun&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L666">❖</a><pre>func (t *<a href="#T">T</a>) Run(name <a href="/builtin#string">string</a>, f func(t *<a href="#T">T</a>)) <a href="/builtin#bool">bool</a></pre></div><p>
执行名字为 name 的子测试 f ，并报告 f 在执行过程中是否出现了任何失败。Run 将一直阻塞直到 f 的所有并行测试执行完毕。
</p>
<p>
Run 可以在多个 goroutine 里面同时进行调用，但这些调用必须发生在 t 的外层测试函数（outer test function）返回之前。
</p>

            
  

          
            <h4 id="T.Skip" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L534">Skip</a> <a class="permalink" href="#T.Skip">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkip&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L534">❖</a><pre>func (c *T) Skip(args ...interface{})</pre></div><p>
调用 Skip 相当于在调用 Log 之后调用 SkipNow 。
</p>

            
  

          
            <h4 id="T.SkipNow" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L552">SkipNow</a> <a class="permalink" href="#T.SkipNow">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkipNow&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L552">❖</a><pre>func (c *T) SkipNow()</pre></div><p>
将当前测试标识为“被跳过”并停止执行该测试。 如果一个测试在失败（参考 Error、Errorf 和 Fail）之后被跳过了， 那么它还是会被判断为是“失败的”。
</p>
<p>
在停止当前测试之后，测试过程将在下一个测试或者下一个基准测试中继续，具体请参考 FailNow 。
</p>
<p>
SkipNow 必须在运行测试的 goroutine 中进行调用，而不能在测试期间创建的 goroutine 中调用。 调用 SkipNow 不会导致其他 goroutine 停止。
</p>

            
  

          
            <h4 id="T.Skipf" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L540">Skipf</a> <a class="permalink" href="#T.Skipf">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkipf&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L540">❖</a><pre>func (c *T) Skipf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
调用 Skipf 相当于在调用 Logf 之后调用 SkipNow 。
</p>

            
  

          
            <h4 id="T.Skipped" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L565">Skipped</a> <a class="permalink" href="#T.Skipped">&para;</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkipped&amp;pkg=testing&amp;repo=">Uses</a></h4>
            <div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L565">❖</a><pre>func (c *T) Skipped() <a href="/builtin#bool">bool</a></pre></div><p>
Skipped 用于报告测试函数是否已被跳过。
</p>

            
  

          
        
          <h3 id="TB" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L379">TB</a> <a class="permalink" href="#TB">&para;</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=TB&amp;pkg=testing&amp;repo=">Uses</a></h3>
          <div class="decl" data-kind="m"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L379">❖</a><pre>type TB interface {
    <span id="TB.Error">Error</span>(args ...interface{})
    <span id="TB.Errorf">Errorf</span>(format <a href="/builtin#string">string</a>, args ...interface{})
    <span id="TB.Fail">Fail</span>()
    <span id="TB.FailNow">FailNow</span>()
    <span id="TB.Failed">Failed</span>() <a href="/builtin#bool">bool</a>
    <span id="TB.Fatal">Fatal</span>(args ...interface{})
    <span id="TB.Fatalf">Fatalf</span>(format <a href="/builtin#string">string</a>, args ...interface{})
    <span id="TB.Log">Log</span>(args ...interface{})
    <span id="TB.Logf">Logf</span>(format <a href="/builtin#string">string</a>, args ...interface{})
    <span id="TB.Name">Name</span>() <a href="/builtin#string">string</a>
    <span id="TB.Skip">Skip</span>(args ...interface{})
    <span id="TB.SkipNow">SkipNow</span>()
    <span id="TB.Skipf">Skipf</span>(format <a href="/builtin#string">string</a>, args ...interface{})
    <span id="TB.Skipped">Skipped</span>() <a href="/builtin#bool">bool</a>
    <span class="com">// contains filtered or unexported methods</span>
}</pre></div><p>
TB 是一个接口，类型 T 和 B 实现了该接口。
</p>

          
          

<h3 id="pkg-subdirectories">Directories <a class="permalink" href="#pkg-subdirectories">&para;</a></h3>
    <table class="table table-condensed">
    <thead><tr><th>Path</th><th>Synopsis</th></tr></thead>
    <tbody><tr><td><a href="testing_iotest.htm">iotest</a><td>Package iotest implements Readers and Writers useful mainly for testing.</td></tr><tr><td><a href="testing_quick.htm">quick</a><td>Package quick implements utility functions to help with black box testing.</td></tr></tbody>
    </table>

<div id="x-footer" class="clearfix">
  <div class="container">
    <a href="http://studygolang.com/" target="_blank">Go语言中文网</a>
    <span class="text-muted">|</span> <a href="http://golang.org/" target="_blank">Go Language</a>
    <span class="pull-right"><a href="#">Back to top</a></span>
  </div>
</div>
<script src="../assets/jquery-2.0.3.min.js"></script>
<script src="../assets/bootstrap.min.js"></script>
<script src="../assets/site.js"></script>
</body>
</html>
